package com.shop2cn.iapi.subscription.testcase;

import org.junit.After;

import com.google.gson.JsonObject;
import com.shop2cn.iapi.subscription.parameter.*;
import com.shop2cn.iapi.subscription.service.*;

import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.dguser.parameter.UserCreateBean;
import com.ymatou.iapi.godmch.parameter.createmchBean;
import com.ymatou.iapi.godmch.service.createmchCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.DGUserCallService;
import com.ymttest.business.service.GodmchSubscriptionService;
import com.ymttest.business.service.shenqi.GodmchManageCallService;
import com.ymttest.database.model.godmch.subscriptioncharge;
import com.ymttest.database.model.godmch.subscriptionfunc;
import com.ymttest.database.sqlwapper.godmch.DefGodMchWapper;
/**
*商家付费状态回调通知Test
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_ChargeNotify{
	private static ChargeNotifyBean chargenotifyBean;
	private static ChargeNotifyCall chargenotifyCall;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("商家付费状态回调通知Test");
	}

	@Before
	public void caseUp() {
		chargenotifyBean=new ChargeNotifyBean();
		chargenotifyCall=new ChargeNotifyCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(chargenotifyCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_ChargeNotify_001() {
		Logger.start(true,"商家购买付费主播一年，成功");
		try {
			Integer mchId = GodmchManageCallService.CreateAllMsgSellerMch().getMchId();
			GodmchSubscriptionService.ApplyCharge(mchId,0,3);
			subscriptioncharge subscriptioncharge = GodmchSubscriptionService.selectSubscriptionChargeNotPay(mchId).get(0);
			chargenotifyBean.setTradingId(subscriptioncharge.getSubscriptionChargeId());
			chargenotifyBean.setPayTime("20191225144331");
			chargenotifyBean.setPaymentId("20191225144331");
			chargenotifyCall.setData(chargenotifyBean);
			chargenotifyCall.callService();			
			Logger.verifyEquals("200", chargenotifyCall.getString("code"), "验证code");
			
			JsonObject subscriptionFunc = GodmchSubscriptionService.GetSubscription(mchId, 0);
			Logger.verifyEquals(3,subscriptionFunc.get("subscriptionStatus").getAsInt(), "验证subscriptionStatus");
			Logger.verifyEquals(364,subscriptionFunc.get("availableDay").getAsInt(), "验证availableDay");
			Logger.verifyIsContains(true,subscriptionFunc.get("remind").isJsonNull(), "验证remind");
			Logger.verifyEquals(false, subscriptionFunc.get("tryout").getAsBoolean(), "验证tryout");
			subscriptionfunc subscriptionfunc2 = GodmchSubscriptionService.selectSubscriptionFuncByMchId(mchId).get(0);
			Logger.verifyEquals(3,subscriptionfunc2.getSubscriptionStatus(), "验证使用状态");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
		
		
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_ChargeNotify_002() {
		Logger.start(true,"商家购买付费主播一年后再进行试用，有效期13月、成功");
		try {
			Integer mchId = GodmchManageCallService.CreateAllMsgSellerMch().getMchId();
			GodmchSubscriptionService.applyTryout(mchId,0,1);
			Thread.sleep(500);
			GodmchSubscriptionService.ApplyCharge(mchId,0,3);
			Thread.sleep(500);
			subscriptioncharge subscriptioncharge = GodmchSubscriptionService.selectSubscriptionChargeNotPay(mchId).get(0);
			chargenotifyBean.setTradingId(subscriptioncharge.getSubscriptionChargeId());
			chargenotifyBean.setPayTime("20191225144331");
			chargenotifyBean.setPaymentId("20191225144331");
			chargenotifyCall.setData(chargenotifyBean);
			chargenotifyCall.callService();			
			Logger.verifyEquals("200", chargenotifyCall.getString("code"), "验证code");
			
			JsonObject subscriptionFunc = GodmchSubscriptionService.GetSubscription(mchId, 0);
			Logger.verifyEquals(3,subscriptionFunc.get("subscriptionStatus").getAsInt(), "验证subscriptionStatus");
			Logger.verifyEquals(365+29,subscriptionFunc.get("availableDay").getAsInt(), "验证availableDay");
			Logger.verifyIsContains(true,subscriptionFunc.get("remind").isJsonNull(), "验证remind");
			Logger.verifyEquals(true, subscriptionFunc.get("tryout").getAsBoolean(), "验证tryout");
			subscriptionfunc subscriptionfunc2 = GodmchSubscriptionService.selectSubscriptionFuncByMchId(mchId).get(0);
			Logger.verifyEquals(3,subscriptionfunc2.getSubscriptionStatus(), "验证使用状态");
		} catch (Exception e) {
			Logger.fail(e);
		}
}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_ChargeNotify_003() {
		Logger.start(true,"商家试用后购买付费主播三年，有效期36月、成功");
		try {
			Integer mchId = GodmchManageCallService.CreateAllMsgSellerMch().getMchId();
			GodmchSubscriptionService.applyTryout(mchId,0,1);
			
			for (int i = 0; i < 3; i++) {
			Thread.sleep(500);
			GodmchSubscriptionService.ApplyCharge(mchId,0,3);
			Thread.sleep(500);
			subscriptioncharge subscriptioncharge = GodmchSubscriptionService.selectSubscriptionChargeNotPay(mchId).get(0);
			chargenotifyBean.setTradingId(subscriptioncharge.getSubscriptionChargeId());
			chargenotifyBean.setPayTime("20191225144331");
			chargenotifyBean.setPaymentId("20191225144331");
			chargenotifyCall.setData(chargenotifyBean);
			chargenotifyCall.callService();			
			Logger.verifyEquals("200", chargenotifyCall.getString("code"), "验证code");
			
			}
			
			JsonObject subscriptionFunc = GodmchSubscriptionService.GetSubscription(mchId, 0);
			Logger.verifyEquals(3,subscriptionFunc.get("subscriptionStatus").getAsInt(), "验证subscriptionStatus");
			Logger.verifyEquals(365*3+29,subscriptionFunc.get("availableDay").getAsInt(), "验证availableDay");
			Logger.verifyIsContains(true,subscriptionFunc.get("remind").isJsonNull(), "验证remind");
			Logger.verifyEquals(true, subscriptionFunc.get("tryout").getAsBoolean(), "验证tryout");
			subscriptionfunc subscriptionfunc2 = GodmchSubscriptionService.selectSubscriptionFuncByMchId(mchId).get(0);
			Logger.verifyEquals(3,subscriptionfunc2.getSubscriptionStatus(), "验证使用状态");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
}
	
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_ChargeNotify_004() {
		Logger.start(true,"商家购买自营商品，成功");
		//商家付费成功后调用更新商户渠道商身份updateChannelsFlag接口，godmch channls=1
		try {
			UserCreateBean usercreateBean = DGUserCallService.initUsercreateBean();
			int mchId_agentId = DGUserCallService.UserCreateCall(usercreateBean);
			
			createmchBean createmchBean=GodmchManageCallService.InitcreatemchBeanForChannel();
			createmchCall createmchCall=GodmchManageCallService.createmchCallForChannel(createmchBean);
			
			createmchBean=GodmchManageCallService.InitcreatemchBeanForAgent(createmchBean.getMchId(), mchId_agentId).get(0);
			createmchBean.setCountry("CHN");
			GodmchManageCallService.createmchCallForChannel(createmchBean);
			Integer mchId=mchId_agentId;
			
			GodmchSubscriptionService.ApplyCharge(mchId,6,8);
			subscriptioncharge subscriptioncharge = GodmchSubscriptionService.selectSubscriptionChargeNotPay(mchId).get(0);
			chargenotifyBean.setTradingId(subscriptioncharge.getSubscriptionChargeId());
			chargenotifyBean.setPayTime("20200915144331");
			chargenotifyBean.setPaymentId(subscriptioncharge.getPaymentId());
			chargenotifyCall.setData(chargenotifyBean);
			chargenotifyCall.callService();			
			Logger.verifyEquals("200", chargenotifyCall.getString("code"), "验证code");
			
			JsonObject subscriptionFunc = GodmchSubscriptionService.GetSubscription(mchId, 0);
			Logger.verifyEquals(0,subscriptionFunc.get("subscriptionStatus").getAsInt(), "验证subscriptionStatus");
			Logger.verifyEquals(0,subscriptionFunc.get("availableDay").getAsInt(), "验证availableDay");
			Logger.verifyIsContains(true,subscriptionFunc.get("remind").isJsonNull(), "验证remind");
			Logger.verifyEquals(false, subscriptionFunc.get("tryout").getAsBoolean(), "验证tryout");
			subscriptionfunc subscriptionfunc2 = GodmchSubscriptionService.selectSubscriptionFuncByMchId(mchId).get(0);
			Logger.verifyEquals(3,subscriptionfunc2.getSubscriptionStatus(), "验证使用状态");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
		
}